package com.hxs.learn.service.core;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hxs.learn.common.entity.BillSequenceKey;
import com.hxs.learn.common.page.Page;
import com.hxs.learn.common.page.PageHelper;
import com.hxs.learn.common.page.PageWrapper;
import com.hxs.learn.common.result.Result;
import com.hxs.learn.common.web.LoginInfo;
import com.hxs.learn.constants.core.sql.SqlColConts;
import com.hxs.learn.entity.core.SysMenuPermission;
import com.hxs.learn.entity.core.SysRoleMenuPermission;
import com.hxs.learn.mapper.core.SysMenuPermissionMapper;
import com.hxs.learn.mapper.core.SysRoleMenuPermissionMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.hxs.learn.entity.core.SysPermission;
import com.hxs.learn.mapper.core.SysPermissionMapper;
import org.springframework.util.StringUtils;

/**
 * @author huxinsheng
 * @data 2018-04-24
 */
@Service
public class SysPermissionService {

    private Logger logger = LoggerFactory.getLogger(SysPermissionService.class);

    private final SysPermissionMapper permissionMapper;

    private final SysMenuPermissionMapper menuPermissionMapper;

    private final SysRoleMenuPermissionMapper roleMenuPermissionMapper;

    private final BillService billService;

    @Autowired
    public SysPermissionService(SysPermissionMapper sysPermissionMapper, BillService billService, SysMenuPermissionMapper menuPermissionMapper, SysRoleMenuPermissionMapper roleMenuPermissionMapper) {
        this.permissionMapper = sysPermissionMapper;
        this.billService = billService;
        this.menuPermissionMapper = menuPermissionMapper;
        this.roleMenuPermissionMapper = roleMenuPermissionMapper;
    }

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param sysPermission 分页条件对象
     * @return PageWrapper<SysPermission>
     */
    public PageWrapper<SysPermission> findSysPermissionPageData(Page page, SysPermission sysPermission) {
        return PageHelper.getPageData(permissionMapper, page, sysPermission);
    }

    /**
     * 查询所有
     *
     * @return List<SysPermission>
     */
    public List<SysPermission> findAllSysPermission() {
        return permissionMapper.selectList(new EntityWrapper<>(new SysPermission()));
    }

    /**
     * 按主键查询
     *
     * @param id 主键id
     * @return SysPermission
     */
    public SysPermission findById(Serializable id) {
        return permissionMapper.selectById(id);
    }

    /**
     * 保存
     *
     * @param loginInfo     登录用户
     * @param sysPermission 系统权限对象
     */
    @Transactional(rollbackFor = Exception.class)
    public Result save(LoginInfo loginInfo, SysPermission sysPermission) {
        sysPermission.setUpdateBy(loginInfo.getLoginName());
        sysPermission.setUpdateTime(new Date());
        Wrapper<SysPermission> wrapper = new EntityWrapper<>();
        if (StringUtils.isEmpty(sysPermission.getId())) {
            wrapper.eq(SqlColConts.NAME, sysPermission.getName());
        } else {
            wrapper.eq(SqlColConts.NAME, sysPermission.getName()).and().ne(SqlColConts.ID, sysPermission.getId());
        }
        int count = permissionMapper.selectCount(wrapper);
        if (count > 0) {
            return Result.error("权限名称已存在!");
        }
        if (StringUtils.isEmpty(sysPermission.getId())) {
            String id = billService.newBillNo(new BillSequenceKey(SqlColConts.KEY_PERMISSION_ID));
            sysPermission.setId(id);
            sysPermission.setCreator(loginInfo.getLoginName());
            sysPermission.setCreateTime(new Date());
            permissionMapper.insertAllColumn(sysPermission);
        } else {
            permissionMapper.updateById(sysPermission);
        }
        return Result.ok();
    }

    /**
     * 按主键删除
     *
     * @param id 主键id
     */
    @Transactional(rollbackFor = Exception.class)
    public Result removeById(Serializable id) {
        //删除角色菜单权限关联数据
        Wrapper<SysRoleMenuPermission> rmpWrapper = new EntityWrapper<>();
        rmpWrapper.eq(SqlColConts.PERMISSION_ID, id);
        roleMenuPermissionMapper.delete(rmpWrapper);
        //删除角色菜单关联数据
        Wrapper<SysMenuPermission> mpWrapper = new EntityWrapper<>();
        mpWrapper.eq(SqlColConts.PERMISSION_ID, id);
        menuPermissionMapper.delete(mpWrapper);

        //删除权限数据
        permissionMapper.deleteById(id);
        return Result.ok();
    }
}
